using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RideMe.FindAndReplace;
using RideMe.Presentation.Properties;
using RideMe.Core;

namespace RideMe.Presentation
{
    public partial class FindResultsWindow : UserControl
    {
        public FindResultsWindow()
        {
            InitializeComponent();
        }

        public void DisplayResults(string pattern, List<SearchResult> results)
        {
            resultsTreeView.Nodes.Clear();
            AddRootNode(pattern, results.Count);
            AddResultsNodes(results);
            resultsTreeView.ExpandAll();
        }

        private void AddRootNode(string pattern, int resultCount)
        {
            resultsTreeView.Nodes.Add(string.Format(Resources.ResultsRootNodeText, pattern, resultCount));
        }

        private void AddResultsNodes(List<SearchResult> results)
        {
            foreach (SearchResult result in results)
            {
                TreeNode subNode = new TreeNode(result.ToString());
                subNode.Tag = result;
                resultsTreeView.Nodes[0].Nodes.Add(subNode);
            }
        }

        private void resultsTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            SearchResult result = e.Node.Tag as SearchResult;

            if (result != null)
            {
                ProjectService.AddOpenFile(result.FileName, result.Offset, result.Length);
            }
        }
    }
}
